package lk.mrt.cse.wihidum.processor;

import org.apache.axiom.soap.*;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.log4j.Logger;
import lk.mrt.cse.wihidum.storage.*;
import lk.mrt.cse.wihidum.util.WSEventingConstants;
import lk.mrt.cse.wihidum.util.*;

import javax.xml.soap.*;
import javax.xml.namespace.QName;
import java.util.*;
/**
 * Created by IntelliJ IDEA.
 * User: Hasini
 * Date: Mar 30, 2010
 * Time: 12:43:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class GetBrokerProcessor implements Processor
{
    private static final Logger logger = Logger.getLogger (BrokerList.class);

    public SOAPEnvelope processMessage(SOAPEnvelope requestEnvelope)
    {
        logger.info("Processing getBroker request");
        SOAPBody body=requestEnvelope.getBody();
        OMElement getBrokerElement=body.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.GetBroker));
        OMElement identifierElement=getBrokerElement.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.Identifier));
        String clientAddress=identifierElement.getText();
        ClientList.addClientToList(clientAddress);
        
        ArrayList<Broker> returningBrokerList=getBrokerSubList();

        SOAPFactory factory= OMAbstractFactory.getSOAP12Factory();
        SOAPEnvelope responseEnvelope=factory.getDefaultEnvelope();

        OMNamespace wihidumNS=factory.createOMNamespace(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.WHIDUM_NAMESPACE_PREFIX);
        responseEnvelope.declareNamespace(wihidumNS);

        OMElement responseElement=factory.createOMElement(WSEventingConstants.ElementNames.GetBrokerResponse,wihidumNS);
        OMElement brokerListElement=factory.createOMElement(WSEventingConstants.ElementNames.BrokerList,wihidumNS);
        brokerListElement.setText("List of Brokers");
        responseElement.addChild(brokerListElement);
        for(Broker broker:returningBrokerList)
        {
           logger.info("Broker in returning envelope:"+broker.getAddress());
           OMElement brokerElement=factory.createOMElement(WSEventingConstants.ElementNames.Broker,wihidumNS);
           brokerElement.setText(broker.getAddress());

           brokerListElement.addChild(brokerElement);

        }
        SOAPBody responseBody=responseEnvelope.getBody();
        responseBody.addChild(responseElement);
        return responseEnvelope;  //To change body of implemented methods use File | Settings | File Templates.
    }
    public ArrayList<Broker> getBrokerSubList()
    {
          ArrayList<Broker> returningBrokerList=BrokerList.shuffleAndReturnBrokerList();
          return returningBrokerList;
    }
    
   
}
